package com.example.java_gobang.api;

import com.example.java_gobang.model.User;
import com.example.java_gobang.model.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserAPI {

    @Resource
    private UserMapper userMapper;

    @ResponseBody
    @PostMapping("/login")
    public Object login(String username, String password, HttpServletRequest req) {
        //关键操作, 根据username 去数据库中进行查询
        //如果能找到匹配的用户, 并且密码也一致, 就认为登陆成功
        User user = userMapper.selectByName(username);
        System.out.println("[login] username=" + username);
        if(user == null || !user.getPassword().equals(password)) {
            //登录失败
            System.out.println("登录失败");
            return new User();
        }

        HttpSession httpSession = req.getSession(true);
        httpSession.setAttribute("user",user);
        return user;
    }

    @RequestMapping("/register")
    @ResponseBody
    public Object register(String username, String password) {
        //参数校验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            System.out.println("[register] 注册时参数为空");
            return new User();
        }
        try {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            userMapper.insert(user);
            return user;
        }catch (org.springframework.dao.DuplicateKeyException e) {
            User user = new User();
            return user;
        }
    }

    @GetMapping("/userInfo")
    @ResponseBody
    public Object getUserInfo(HttpServletRequest req) {
        try {
            HttpSession httpSession = req.getSession(false);
            User user = (User) httpSession.getAttribute("user");
            //拿着这个user对象去数据库里找到最新的数据
            User newUser = userMapper.selectByName(user.getUsername());
            return newUser;
        }catch (NullPointerException e) {
            return new User();
        }
    }
}
